package com.iwdnb.gkgz.common.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.bmnf.common.utils.HttpClientUtils;
import com.iwdnb.gkgz.common.model.dto.StockDayData;

public class MairuiStockUtils {
    private static final String fscjUrl = "http://api.mairui.club/hsrl/fscj/%s/b997d4403688d5e66a";

    public static List<StockDayData> queryFenshiDataList(String code) {
        code = code.substring(2);
        String date = DateUtil.formatDate(new Date());
        String response = HttpClientUtils.get(String.format(fscjUrl, code));
        JSONArray jsonArray = JSON.parseArray(response);
        List<StockDayData> stockDayDataList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            StockDayData data = new StockDayData();
            data.setDate(date);
            data.setDatetime(jsonObject.getString("d") + " " + jsonObject.getString("t"));
            data.setClosePrice(jsonObject.getBigDecimal("p"));
            data.setOpenPrice(data.getClosePrice());
            data.setMaxPrice(data.getClosePrice());
            data.setMinPrice(data.getClosePrice());
            data.setTradeNum(jsonObject.getInteger("v"));
            stockDayDataList.add(data);

        }
        Collections.reverse(stockDayDataList);
        return stockDayDataList;
    }

    public static void main(String[] args) {
        System.out.println(JSON.toJSONString(queryFenshiDataList("sh600650")));
    }
}
